<?php

namespace  app\wxapp\controller;
use think\Controller;
use think\Request;
use \Firebase\JWT\JWT;
use think\Db;
use app\common\controller\Operate;

/**
* 
*/
class Custom extends Controller
{
	public function login(){
		$request = Request::instance();
		$param = $request->post();
		try{
			$data = Db::name('tmp_user')->where(['user_name'=>$param['user_name']])->find();	
		}catch(\Exception $e){
			return [
                'code'=>2,
                'msg'=>$e->getMessage(),
            ]; 
		}
		if($data){
			$pass = password_verify($param['password'],$data['password']);
			if($pass){
				$key = "token";
				$token = array(
				    "iss" => $data['id'],
				    "car_id"=>$data['car_id'],
				    "iat" => $data['update_time'],
				    "exp" => $data['update_time']+$data['minute']*60
				);
				try{
					$jwt = JWT::encode($token, $key);
				}catch(\Exception $e){
					return [
		                'code'=>2,
		                'msg'=>$e->getMessage()
		            ]; 
				}
				
				return [
					'code' => 1,
					'msg' => '登录成功',
					'jwt'=>$jwt,
					'minute'=>$data['minute']
				];
			}else{
				return [
					'code'=> 10003,
					'msg'=> "密码错误"
				];
			}
		}else{
			return [
				'code'=> 10002,
				'msg'=> "账号不存在"
			];
		}
	}


	public function details(){
		$request = Request::instance();
		$jwt = $request->get();
		$key = "token";
		try{
			$decoded = JWT::decode($jwt['jwt'], $key, array('HS256'));
		}catch(\Exception $e){
			return [
				'code'=>10004,
				'msg'=>$e->getMessage()
			];
		}
		if($decoded){
			$car_id = $decoded->car_id; 
			try{
				$cars = Db::name('cars')->field('car_sn,car_unique_id,supplier_no,car_name,motor_id,car_breed,car_id,equipment_id')->where(['car_id'=>$car_id])->find();
			}catch(\Exception $e){
				return [
					'code'=>10005,
					'msg'=>$e->getMessage()
				];
			}
			if($cars){
				return [
					'code'=>10006,
					'cars'=>$cars
				];
			}	
		}
	}

	public function unlock(){
		$request = Request::instance();
		$param = $request->get();
		$key = "token";
		try{
			$decoded = JWT::decode($param['jwt'], $key, array('HS256'));
		}catch(\Exception $e){
			return [
				'code'=>10004,
				'msg'=>$e->getMessage()
			];
		}
		if($decoded){
			$car_id = $decoded->car_id;
			try{
				$object = new Operate();
				$data= $object->unlock($car_id);

			}catch(\Exception $e){
				return [
					'code'=>13,
					'msg'=>$e->getMessage()
				];
			}
			if($data){
				return $data;
			}
		}
	}
    

    public function lock(){
    	$request = Request::instance();
		$param = $request->get();
		$key = "token";
		try{
			$decoded = JWT::decode($param['jwt'], $key, array('HS256'));
		}catch(\Exception $e){
			return [
				'code'=>10004,
				'msg'=>$e->getMessage()
			];
		}
		if($decoded){
			$car_id = $decoded->car_id;
			try{
				$object = new Operate();
				$data = $object->unlock($car_id);

			}catch(\Exception $e){
				return [
					'code'=>13,
					'msg'=>$e->getMessage()
				];
			}
			if($data){
				return $data;
			}
		}
    }

    public function Ignite(){
    	$request = Request::instance();
		$param = $request->get();
		$key = "token";
		try{
			$decoded = JWT::decode($param['jwt'], $key, array('HS256'));
		}catch(\Exception $e){
			return [
				'code'=>10004,
				'msg'=>$e->getMessage()
			];
		}
		if($decoded){
			$car_id = $decoded->car_id;
			try{
				$object = new Operate();
				$data = $object->unlock($car_id);

			}catch(\Exception $e){
				return [
					'code'=>13,
					'msg'=>$e->getMessage()
				];
			}
			if($data){
				return $data;
			}
		}
    }

    public function unIgnite(){
    	$request = Request::instance();
		$param = $request->get();
		$key = "token";
		try{
			$decoded = JWT::decode($param['jwt'], $key, array('HS256'));
		}catch(\Exception $e){
			return [
				'code'=>10004,
				'msg'=>$e->getMessage()
			];
		}
		if($decoded){
			$car_id = $decoded->car_id;
			try{
				$object = new Operate();
				$data = $object->unlock($car_id);

			}catch(\Exception $e){
				return [
					'code'=>13,
					'msg'=>$e->getMessage()
				];
			}
			if($data){
				return $data;
			}
		}
    } 

    public function whistle(){
    	$request = Request::instance();
		$param = $request->get();
		$key = "token";
		try{
			$decoded = JWT::decode($param['jwt'], $key, array('HS256'));
		}catch(\Exception $e){
			return [
				'code'=>10004,
				'msg'=>$e->getMessage()
			];
		}
		if($decoded){
			$car_id = $decoded->car_id;
			try{
				$object = new Operate();
				$data = $object->unlock($car_id);

			}catch(\Exception $e){
				return [
					'code'=>13,
					'msg'=>$e->getMessage()
				];
			}
			if($data){
				return $data;
			}
		}
    }                                                                                                                                                                                                                               
}